<template>
  <div class="app-content">
    <!-- 顶部tips -->
    <div class="tips" ref="tips">
      <h3>新订单出库日报</h3>
      <!-- <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="订单信息">
          <el-input
            size="small"
            v-model="SearchForm.OrderNo"
            placeholder="请输入搜索的信息"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" type="primary" @click="GetDataList"
            >查询</el-button
          >
          <el-button size="mini" type="success" @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form> -->
    </div>
    <!-- 中间表格部分 -->
    <el-table v-if="tabaleHeight.length" ref="table" :data="tableData" border style="width: 100%" :height="tabaleHeight"
      size="mini" v-loading="loading">
      <el-table-column :label="item.title" :width="item.width" show-overflow-tooltip v-for="item in Page.tableHeader"
        :key="item.title">
        <template v-slot="scope">
          <ColumnTable :CurretItem="scope.row" :ItemValue="item.value" />
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="100">
        <template v-slot="scope">
          <el-button size="mini" type="text" :disabled="scope.row.ship_style == 1"
            @click="UpdataDialog(scope.row, 'UpdataDialog')">修改</el-button>
          <el-button size="mini" type="text" :disabled="scope.row.ship_style == 2"
            @click="DelCurrentItem(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页部分 -->
    <div class="pagination">
      <el-pagination @current-change="handleCurrentChange" :current-page="paramsquery.pageIndex"
        :page-size="paramsquery.pageSize" layout="total, prev, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 全局表单弹窗 -->
    <DialogComponent ref="DialogComponent" :Dialog="Dialog" :Mobile="Mobile" @Submit="Submit" />
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex"; // 引入vuex属性
import mixin from "@/mixins/mixin"; // 引入mixin
import { GetStockOutNewList } from "@/api/index"; // 引入新订单出库列表
import { UpdateStockOut, DelStockDownOut } from "@/api/index"; // 更新新订单出库 删除新订单出库
import { Page } from "./index"; // 引入页面信息
export default {
  mixins: [mixin],
  data() {
    return {
      /* 页面信息 */
      Page,
      /* 搜索部分 */
      SearchForm: {
        OrderNo: "",
      },
      SupplierOptions: [
        {
          id: 0,
          name: "全部",
        },
      ],
      WareHouseOptions: [
        {
          id: 0,
          name: "全部",
        },
      ],
      /* 表格部分 */
      loading: true,
      tableData: [],
      paramsquery: {
        pageIndex: 1,
        pageSize: 30,
      },
      total: 0,
      /* 弹窗的信息 默认指定一个 */
      Dialog: Page.Dialog.UpdataDialog,
    };
  },
  computed: {
    ...mapState(["isMobile", "CurrentItem"]),
  },
  created() { },
  mounted() {
    this.GetDataList();
  },
  watch: {},
  methods: {
    ...mapActions(["SetCurrentItem"]),
    // 请求列表数据
    GetDataList() {
      this.loading = true;
      const data = {
        pageIndex: this.paramsquery.pageIndex,
      };
      GetStockOutNewList(data).then((res) => {
        // console.log(res);
        this.loading = false;
        res.orderList.map((item) => {
          Object.entries(item).forEach(([key, value]) => {
            if (value === null) {
              item[key] = "";
            }
          });
        });
        this.tableData = res.orderList;
        this.pageIndex = res.pageIndex;
        this.pageSize = res.pageSize;
        this.total = res.pageTotal;
      }).catch(err => {
        console.log(err);
        this.loading = false
      })
    },
    // 修改备货入库弹窗
    UpdataDialog(rows, name) {
      this.DialogType = name;
      const { DialogForm, DialogFormType } = this.Page.Dialog[name];
      DialogFormType.forEach((item) => {
        DialogForm[item.value] = rows[item.value]
          ? String(rows[item.value])
          : "";
      });
      this.SetCurrentItem(rows);
      this.Dialog = this.Page.Dialog[name];
      this.$refs.DialogComponent.openModal(name);
    },
    // 确认弹窗的信息
    Submit(e) {
      // console.log(e);
      if (e.DialogTtile == "修改新订单出库") this.UpdataInfo(e.form);
    },
    // 确认修改
    UpdataInfo(e) {
      // console.log(e);
      const data = {
        id: this.CurrentItem.id,
        stock_in_id: e.stock_in_id,
        new_order_id: e.new_order_id,
        new_order_sku_num: e.new_order_sku_num,
        exchange_order_id: null,
        exchange_order_num: null,
      };
      Object.entries(data).forEach(([key, value]) => {
        if (value == "") {
          data[key] = null;
        }
      });
      // console.log(data, "data");
      UpdateStockOut(data).then((res) => {
        // console.log(res);
        if (res.msg) {
          this.$message({
            message: "修改成功",
            type: "success",
          });
          this.GetDataList();
          this.$refs.DialogComponent.closeModal();
        } else {
          this.$message.error("修改失败");
        }
      });
    },
    // 删除新订单出库日报
    DelCurrentItem(e) {
      // console.log(e);
      this.$confirm(`您确定要删除本条数据吗？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DelStockDownOut(e.id).then((res) => {
            if (res.msg) {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.GetDataList();
            } else {
              this.$message.error("删除成功");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  components: {},
};
</script>

<style scoped lang="less">
@import url(../../../style/index.css);

/deep/ .el-form-item {
  margin-bottom: 0;
}
</style>
